package cn.yjh.utils;

import cn.yjh.support.Templates;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.io.ByteArrayOutputStream;
import java.net.URL;

/**
 * @DESC:
 * @AUTHOR: YouJinhua
 * @DATE: 2021-12-04 11:39:14
 * @VERSION: 1.0
 */
public class XMLUtils {

    /**
     * JavaBean转换成xml
     * 默认编码UTF-8
     * @param obj
     * @return
     */
    public static String convertToXml(Object obj) {
        return convertToXml(obj, "UTF-8");
    }

    /**
     * JavaBean转换成xml
     * @param obj
     * @param encoding
     * @return
     */
    public static String convertToXml(Object obj, String encoding) {
        String result = null;
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

            StringWriter writer = new StringWriter();
            marshaller.marshal(obj, writer);
            result = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * xml转换成JavaBean
     * @param xml
     * @param c
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T converyToJavaBean(String xml, Class<T> c) {
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (T) unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return t;
    }

    public static void main(String[] args) throws Exception {
        URL url = ClassLoader.getSystemResource("templates.xml");
        InputStream input = url.openStream();

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        IOUtils.copy(input, output);
        Templates templates = converyToJavaBean(output.toString("UTF-8"), Templates.class);
        System.out.println(templates.getTemplates()[0].getTemplateSource());

    }
}
